<script setup>
import { ref } from 'vue'

const props = defineProps({
  title: {
    type: String,
    default: '添加视频'
  },
  videoData: {
    type: Object,
    default: {
      vCoverImg: '',
      vLink: '',
      vTitle:'',
      proLink: '',
      status: '',
      createtime: new Date().toLocaleDateString()
    },
    required: false
  }
})
const video= ref(props.videoData)
const drawer = ref(false)
defineExpose({
  drawer
})
const beforeAvatarUpload = (row) => {
}
const uploadImg = (row) => {
  //TODO 发送请求
  video.value.vCoverImg = URL.createObjectURL(row.file)
}

const emit = defineEmits(['onSubmit'])
/**
 *  提交修改
 */
const onSubmit = () => {
  emit('onSubmit', video.value)
}
const onCancel = () => {
  drawer.value = false
  video.value = props.goodsData
  ElMessage.info('已取消')
}
</script>

<template>
  <el-drawer v-model="drawer">
    <template #header>
      <div style="display: flex;align-items: center;gap: 10px">
        <el-icon :color="'#39c5bb'" size="24">
          <CirclePlus />
        </el-icon>
        <h2>{{ title }}</h2>
      </div>
    </template>
    <el-form>
      <el-form-item label="视频封面" required>
        <el-upload
          :before-upload="beforeAvatarUpload"
          :http-request="uploadImg"
          :show-file-list="false"
          class="uploader"
        >
          <img v-if="video.vCoverImg" :src="video.vCoverImg" class="avatar" />
          <el-icon v-else :size="50" color="#ccc"
                   style="border: #cccccc dashed 1px;border-radius: 5px;width: 100px;height: 100px">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="视频链接" required>
        <el-input v-model="video.proLink" placeholder="请输入" style="width: 300px" />
      </el-form-item>
      <el-form-item label="视频标题" required>
        <el-input v-model="video.vTitle" placeholder="请输入" style="width: 300px" />
      </el-form-item>
      <el-form-item label="商品状态" required>
        <el-radio-group v-model="video.status" style="width: 300px">
          <el-radio :value="1" label="正常" />
          <el-radio :value="0" label="下架" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-text>{{ video.createtime }}</el-text>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button plain type="info" @click="onCancel">取消</el-button>
      <el-button :color="'#39c5bb'" plain @click="onSubmit">确认</el-button>
    </template>
  </el-drawer>
</template>

<style lang="scss" scoped>
.avatar {
  width: 100px;
  height: 100px;
  border-radius: 10px;
  display: block;
}
</style>